import * as types from '../actions/actionTypes';
const initialState = {
    cartItems: [],
    cart_num: 0,
    app_cart_cookie_id: '',
    isLoading: true,
    cart_debug:0,
    total_price:0,
};

/**
 * 购物车reducer
 * @param state
 * @param action
 * @returns {*}
 */
let cartReducer = (state = initialState, action) => {
    switch (action.type) {
        case types.kAppCartCookieIdFromSync:
            return {
                ...state,
                ...action,
            };
        case types.kCartNumFromSync:
            return {
                ...state,
                ...action,
            };
        case types.kCartView:
            return {
                ...state,
                isLoading: true,
            };
            
        case types.kCartNumReceived:
            return {
                ...state,
                cart_num: action.cart_num,
                isLoading: false,
            };

        case types.kCartViewReceived:
            return {
                ...state,
                ...action,
                isLoading: false,
                cartItems: action.cartItems,
                cart_num: action.cart_num,
            };
        case types.kCartAdd:
            return {
                ...state,
                isLoading: true,
            };
        case types.kCartAddReceived:
            return {
                ...state,
                ...action,
                isLoading: false,
            };
        case types.kCartItemReceived:
            return {
                ...state,
                ...action,
            };
        // case types.kCartAdjust:
        //     let cartItems=state.cartItems;
        //     let product_id=action.product_id;
        //     let adjustType=action.adjustType;
        //     let newItems=[];
        //     let new_cart_num=state.cart_num;

        //     console.log('<cartItem>:'+JSON.stringify(cartItems)+'</cartItems>');
        //     for (var i = cartItems.length - 1; i >= 0; i--) 
        //     {
        //         let tmpItem=cartItems[i];
        //         if(tmpItem.product.product_id==product_id)
        //         {
        //             if(adjustType=='add')
        //             {
        //                 tmpItem.count=tmpItem.count+1;
        //                 new_cart_num+=1;
        //             }
        //             else{
        //                 tmpItem.count=tmpItem.count-1;
        //                 new_cart_num-=1;
        //             }

        //             if(tmpItem.count>0)newItems.put(tmpItem);
        //         }
        //         else{
        //             newItems.put(tmpItem);
        //         }
                
        //     }

            
        //     console.log('<newCartItem>:'+JSON.stringify(newItems)+'</newCartItem>');
        //     return {
        //         ...state,
        //         cartItems: newItems,
        //         cart_num: new_cart_num,
        //         cart_debug:123456,
        //     };

        default:
            return state;
    }
};

export default cartReducer;